<template>
  <div class="user-container">
    <div class="dept-tree-wrap">
      <el-input v-model="filterText" size="mini"></el-input>
      <el-tree
          v-loading="treeLoading"
          :data="deptTree"
          node-key="id"
          ref="deptTreeRef"
          :expand-on-click-node="false"
          highlight-current
          @current-change="currentNodeChange"
          :filter-node-method="filterDeptTree"
          :props="{label:'name'}"
          default-expand-all>
      </el-tree>
    </div>
    <div class="user-wrap">
      <div class="search-box">
        <el-form :model="searchForm" ref="searchFormRef" size="mini" inline label-suffix="：" >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="searchForm.username"></el-input>
          </el-form-item>
          <el-form-item label="手机" prop="phone">
            <el-input v-model="searchForm.phone"></el-input>
          </el-form-item>
          <el-form-item label="关联角色" prop="roleCodes">
            <el-select v-model="searchForm.roleCodes" multiple collapse-tags>
              <el-option :label="item.label" :value="item.value" :key="index"  v-for="(item,index) in roleDict"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="dataStatus">
            <el-select v-model="searchForm.dataStatus">
              <el-option :label="item.label" :value="item.value" :key="index"  v-for="(item,index) in dataStatusOptions"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间" prop="createTimeRange">
            <el-date-picker v-model="searchForm.createTimeRange" style="width: 200px" type="daterange" format="YYYY-MM-DD"></el-date-picker>
          </el-form-item>
          <el-form-item label="修改时间" prop="updateTimeRange">
            <el-date-picker v-model="searchForm.updateTimeRange" style="width: 200px" type="daterange" format="YYYY-MM-DD"></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button  @click="resetSearchForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="btn-wrapper">
        <el-button size="small" @click="addUser">新建</el-button>
      </div>
      <div class="table-wrapper">
        <el-table :data="tableData" size="mini" v-loading="tableLoading" border stripe>
          <el-table-column type="index" label="序号" width="80" align="center"></el-table-column>
          <el-table-column label="用户名" prop="username"></el-table-column>
          <el-table-column label="用户昵称" prop="nickname"></el-table-column>
          <el-table-column label="手机" prop="phone"></el-table-column>
          <el-table-column  label="状态" prop="dataStatus">
            <template v-slot="{row}">
              <div>{{translateDictCode(row.dataStatus,dataStatusOptions)}}</div>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="createTime"></el-table-column>
          <el-table-column label="修改时间" prop="updateTime"></el-table-column>
          <el-table-column label="操作" width="150" fixed="right">
            <template v-slot="{row}">
              <el-button type="text" @click="editUser(row.id)">修改 </el-button>
              <el-button type="text" @click="delUser(row.id)">删除 </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="page-wrapper">
        <el-pagination
            :current-page="searchForm.page"
            :page-size="searchForm.pageSize"
            :page-sizes="[10, 20, 30, 40,50]"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @current-change="handleCurrentPageChange"
            @size-change="handlePageSizeChange"
        ></el-pagination>
      </div>
    </div>
    <el-dialog title="新增/修改" width="600px" v-model="visible" destroy-on-close :close-on-click-modal="false" @close="resetUserForm">
      <el-form :model="userForm" size="mini" :rules="rules" ref="userFormRef" label-width="100px" label-suffix="：">
        <el-form-item  label="用户部门">
          <tree-select :clearable="false" v-model="userForm.deptId" :options="treeSelectOptions"></tree-select>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="userForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码"  :prop="!isEdit ? 'password' :'passwordEdit'">
          <el-input type="password" v-model="userForm.password"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input v-model="userForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="userForm.nickname"></el-input>
        </el-form-item>
        <el-form-item label="头像" >
          <img-cutter @cutDown="cutDown" :cutWidth="70" :cutHeight="70" :sizeChange="false">
            <template #open>
              <el-avatar  :size="50" icon="el-icon-user-solid" :src="serviceUrl+userForm.avatar"></el-avatar>
            </template>
          </img-cutter>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="状态" prop="dataStatus">
              <el-select v-model="userForm.dataStatus">
                <el-option :label="item.label" :value="item.value" :key="index"  v-for="(item,index) in dataStatusOptions"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="关联角色">
              <el-select v-model="userForm.roleIds" multiple clearable>
                <el-option :label="item.label" :value="item.value" :key="index" v-for="(item,index) in roleDictOptions"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button type="primary" size="small" @click="submit">提交</el-button>
        <el-button type="danger" size="small" @click="visible = false">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import {defineComponent, computed} from 'vue'
import {useStore} from 'vuex'
import useDeptTree from "@/views/system/user/hooks/useDeptTree";
import useSearch from "@/views/system/user/hooks/useSearch";
import useDataStatusOptions from "@/hooks/useDataStatusOptions/useDataStatusOptions";
import useTable from "@/views/system/user/hooks/useTable";
import useTranslateDictCode from "@/hooks/useTranslateDictCode/useTranslateDictCode";
import useForm from "@/views/system/user/hooks/useForm";
import ImgCutter from "@/components/ImgCutter";
import TreeSelect from "vue-treeselect-next"
import "vue-treeselect-next/dist/vue-treeselect.css"
import {serviceUrl} from "@/utils/request"
export default defineComponent({
  name: 'User',
  components:{ImgCutter,TreeSelect},
  setup() {
    const $store = useStore()
    function addCount() {
      $store.dispatch('setCount', $store.state.count + 1)
    }
    const count = computed(() => {
      return $store.state.count
    })
    console.log(count.value)
    const {deptTree, filterText, filterDeptTree, currentNodeChange,treeLoading,treeSelectOptions} = useDeptTree();
    const {roleDict,getRoleBuildDict,handleSearch,resetSearchForm} = useSearch()
    const {getUserDataList,tableData,total ,tableLoading,handleCurrentPageChange,handlePageSizeChange} = useTable()
    getRoleBuildDict()
    getUserDataList()
    return {
      treeSelectOptions,
      count,
      addCount,
      deptTree,
      filterText,
      filterDeptTree,
      currentNodeChange,
      ...useTranslateDictCode(),
      ...useDataStatusOptions(),
      ...useSearch(),
      ...useForm(),
      roleDict,
      handleSearch,
      resetSearchForm,
      treeLoading,
      tableData,
      total,
      tableLoading,
      handleCurrentPageChange,
      handlePageSizeChange,
      serviceUrl
    }
  }
})
</script>
<style lang="scss" scoped>
.user-container {
  height: 100%;
  padding: 5px;
  box-sizing: border-box;
  display: flex;
  .dept-tree-wrap {
    width: 300px;
    min-width: 300px;
    padding: 5px;
    padding-right: 10px;
    box-sizing: content-box;
  }
  .user-wrap{
    overflow: auto;
  }
}
</style>
